﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace TamerlaneData
{
    public class FileParser
    {

        /// <summary>
        /// Loads map data from text file (IE province name and rgb value).
        /// </summary>
        /// <param name="filename">The file to read.</param>
        /// <returns>A list of provinces in the text-file</returns>
        public static List<Province> LoadMapText(String filename)
        {
            List<Province> provinces = new List<Province>();

            //Load provinces
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(filename))
                {
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine("DEBUG! " + line);
                        //Check for comments
                        if (line.StartsWith("#"))
                            continue;

                        string[] delim = { ";" };
                        string[] result = line.Split(delim, StringSplitOptions.None);

                        //Has something correct been read?
                        if (result.Length < 4)
                            continue;

                        provinces.Add(new Province(result[3], result[0] + "," + result[1] + "," + result[2]));
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
                throw e;
            }

            //return text data
            return provinces;
        }

        /// <summary>
        /// Loads and returns a bitmap with the given name.
        /// </summary>
        /// <param name="filename">Name of map image to load.</param>
        /// <returns>A bitmap.</returns>
        public static Bitmap LoadMapImage(string filename)
        {
            return new Bitmap(filename);
        }

        /// <summary>
        /// Loads a scenario from a text file.
        /// </summary>
        /// <param name="filename">The name of the scenarion file.</param>
        /// <returns>Returns a game.</returns>
        public static Game LoadScenario(string filename)
        {

            Game game = new Game();

            //Load province data
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(filename))
                {
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        char[] delim = { '_' };
                        string[] result = line.Split(delim);

                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
                throw e;
            }

            return game;

        }

    }
}
